package com.lsp.microuser.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.lsp.microcommon.constants.SmsEnum;
import com.lsp.microcommon.entity.User;
import com.lsp.microcommon.exception.CommonException;
import com.lsp.microcommon.utils.AliSmsUtils;
import com.lsp.microcommon.utils.JwtUtils;
import com.lsp.microcommon.utils.RedisUtils;
import com.lsp.microuser.user.dao.UserDao;
import com.lsp.microuser.user.params.LoginParam;
import com.lsp.microuser.user.params.RegisterParam;
import com.lsp.microuser.user.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private UserDao userDao;

    @Resource
    ThreadPoolExecutor threadPoolExecutor;

    @Override
    public String login(LoginParam param) {
        User user = userDao.selectByUserName(param.getUsername());
        if (user == null) {
            throw new CommonException("账号错误");
        }
        String md5Password = SecureUtil.md5(param.getPassword());
        if (!user.getPassword().equals(md5Password)) {
            throw new CommonException("密码错误");
        }

        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getId());
        //使用jwt做token不需要将jwt作为key存到redis中
        //jwt中已经存了用户信息，也能设置过期时间

        return JwtUtils.createJwt(map);
    }

    @Override
    public String sendCode(String phone) {
        String code = RandomUtil.randomNumbers(4);
        //正则表达式判断电话号是否合法
        if (!phone.matches("^1[3-9]\\d{9}$")) {
            throw new CommonException("请输入正确的手机号");
        }
        User user = userDao.selectByPhone(phone);
        if (user != null) {
            throw new CommonException("手机号已注册");
        }
        //  存储验证码，用于判断输入的验证码是否正确
        //  存入redis，考虑key value
        RedisUtils.setValueTimeout("phone", phone, 120);
        RedisUtils.setValueTimeout(phone, code, 120);
        //  通过线程池异步发送验证码
        threadPoolExecutor.execute(() -> AliSmsUtils.sendSms(SmsEnum.SMS_LOGIN.getSignName(),
                SmsEnum.SMS_LOGIN.getTemplateCode(),
                phone,
                code));
        System.out.println(code);
        return code;
    }

    @Override
    public void addUser(RegisterParam param) {
        if(!param.getIsPassing().equals("yes")){
            throw new CommonException("验证码验证不通过");
        }
        String phone = (String) RedisUtils.getValue("phone");
        if (!phone.equals(param.getPhone())) {
            throw new CommonException("请输入发送验证码的电话号");
        }
        //先看数据库有没有这个用户名
        User userInfo = userDao.selectByUserName(param.getUsername());
        if (userInfo != null) {
            throw new CommonException("用户名已存在");
        }
        //把信息存入数据库
        User user = new User();
        //把密码做md5编码
//        String md5Password = SecureUtil.md5(param.getPassword());
//        param.setPassword(md5Password);
        BeanUtils.copyProperties(param, user);
        userDao.insert(user);
    }
}
